/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.plan.service;

import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.javaeye.plan.dto.InStoreVoucher;
import com.javaeye.plan.dto.InStoreVoucherDetail;
import com.javaeye.store.dao.MaterialsBatchDetailDAO;
import com.javaeye.store.dao.MaterialsChangeDetailDAO;
import com.javaeye.store.dao.MaterialsSKUDetailDAO;
import com.javaeye.store.dto.MaterialsBatchDetail;
import com.javaeye.store.dto.MaterialsChangeDetail;
import com.javaeye.store.dto.MaterialsSKUDetail;
import com.javaeye.common.util.DateUtils;

public class VoucherInStoreService extends InStoreVoucherService {
	
	protected static Log log = LogFactory.getLog(InStoreVoucherCheckService.class);
	
	private MaterialsChangeDetailDAO materialsChangeDetailDao;
	
	private MaterialsSKUDetailDAO materialsSKUDetailDao;
	
	private MaterialsBatchDetailDAO materialsBatchDetailDao;
	
	public void setMaterialsChangeDetailDao(
			MaterialsChangeDetailDAO materialsChangeDetailDao) {
		this.materialsChangeDetailDao = materialsChangeDetailDao;
	}

	public void setMaterialsSKUDetailDao(MaterialsSKUDetailDAO materialsSKUDetailDao) {
		this.materialsSKUDetailDao = materialsSKUDetailDao;
	}

	public void setMaterialsBatchDetailDao(
			MaterialsBatchDetailDAO materialsBatchDetailDao) {
		this.materialsBatchDetailDao = materialsBatchDetailDao;
	}

	public void saveInStoreVoucher(InStoreVoucher voucher) {
		voucher.setStatus(InStoreVoucher.VOUCHES_STATES_END);
		voucher.setUpdateDate(DateUtils.now());
		dao.saveInStoreVoucher(voucher);
		
		
		Set<InStoreVoucherDetail> details = voucher.getVoucherDetails();
		for (InStoreVoucherDetail detail : details) {
			int productId = detail.getProductId();
			//主要此次仓库只收合格数
			//插入库存变动明细表
			float acceptNumber = detail.getAcceptNumber();
			if (acceptNumber > 0) {
				MaterialsChangeDetail changeDetail = new MaterialsChangeDetail();
				changeDetail.setMaterialsId(productId);
				changeDetail.setVoucherNo(voucher.getId());
				changeDetail.setChangeType(MaterialsChangeDetail.PLAN_INSTORE);
				changeDetail.setBatchNo(detail.getBatchNo());
				changeDetail.setNumber(acceptNumber);
				materialsChangeDetailDao.save(changeDetail);
			}
			/*float otherNumber = detail.getOtherNumber();
			if (otherNumber > 0) {
				MaterialsChangeDetail changeDetail = new MaterialsChangeDetail();
				changeDetail.setMaterialsId(productId);
				changeDetail.setVoucherNo(voucher.getId());
				changeDetail.setChangeType(MaterialsChangeDetail.PLAN_INSTORE);
				changeDetail.setBatchNo(detail.getBatchNo());
				changeDetail.setNumber(otherNumber);
				materialsChangeDetailDao.save(changeDetail);
			}
			*/
			//更新实时库存数量
			MaterialsSKUDetail skuDetail = materialsSKUDetailDao.getMaterialsSKUDetail(productId);
			if (acceptNumber > 0) {
				skuDetail.setNumber(skuDetail.getNumber() + acceptNumber);
			}
			/*if (otherNumber > 0) {
				skuDetail.setDefectiveNumber(skuDetail.getDefectiveNumber() + otherNumber);
			}*/
			materialsSKUDetailDao.saveMaterialsSKUDetail(skuDetail);
			
			//更新批次库存数量
			MaterialsBatchDetail batchDetail = materialsBatchDetailDao.getBatchDetail(productId, detail.getBatchNo());
			if (acceptNumber > 0) {
				batchDetail.setNumber(batchDetail.getNumber() + detail.getAcceptNumber());
			}
			/*if (otherNumber > 0) {
				
			}*/
			materialsBatchDetailDao.save(batchDetail);
		}
	}
}
